package com.fuelwallet.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.SearchRecentSuggestions;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.fuelwallet.R;
import com.fuelwallet.model.IconListAdapter;
import com.fuelwallet.model.MySuggestionProvider;
import com.fuelwallet.model.PriceUtility;
import com.fuelwallet.model.SearchedPlacesHistoryHandle;

public class SettingActivity extends ListActivity {
	public static final int DIALOG_PROGRESS = 10;
	
	private SearchedPlacesHistoryHandle searchHistoryHandle;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.setting);
		setTitle("Setting");		
		
		List<String> setting_option = new ArrayList<String>();
		setting_option.add("Account Manager");		
		setting_option.add("Update Prices");
		setting_option.add("Price List");
		setting_option.add("Show History");		
		setting_option.add("Clear Places");
		
		searchHistoryHandle = new SearchedPlacesHistoryHandle(this);

		IconListAdapter adapter = new IconListAdapter(this,
				R.layout.icon_listitem, setting_option);

		setListAdapter(adapter);
	}

	private Handler updateHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch(msg.what){
			case PriceUtility.MESSAGE_UPDATE_OK:
				//setProgressBarIndeterminateVisibility(false);
				SettingActivity.this.dismissDialog(DIALOG_PROGRESS);
				break;			
			}
			return;
		}
	};
	
	protected Dialog onCreateDialog(int id){
		switch(id){
		case DIALOG_PROGRESS:
			ProgressDialog progressDialog = new ProgressDialog(this);
			progressDialog.setTitle(R.string.dialog_title_update_price);
			progressDialog.setMessage(getResources().getString(R.string.dialog_message_update_price));
			progressDialog.setIndeterminate(true);
    		progressDialog.setCancelable(true);
    		return progressDialog;
    	default:
    		return null;
		}
	}

	protected void onListItemClick(ListView l, View v, int position, long id) {

		String option = l.getItemAtPosition(position).toString();
		if (option.equals("Account Manager")) {
			Intent intent = new Intent(SettingActivity.this,
					CarProfileManagerActivity.class);
			startActivity(intent);
			overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
		} else if (option.equals("Update Prices")) {
			//setProgressBarIndeterminateVisibility(true);	
			SettingActivity.this.showDialog(DIALOG_PROGRESS);
			if(isNetworkOnline() == true){			
				PriceUtility priceutility = new PriceUtility(this, updateHandler);
				priceutility.startExecution();
				priceutility.analyze();
				priceutility.updateDatabase(true);		// message is sent here
			}else{				
				SettingActivity.this.dismissDialog(DIALOG_PROGRESS);
				Toast.makeText(SettingActivity.this, "Please enable network connection first! ", 
						Toast.LENGTH_LONG).show();				
			}
		} else if (option.equals("Price List")) {
			Intent intent3 = new Intent(SettingActivity.this,
					ShowPriceActivity.class);
			startActivity(intent3);
			overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
		} else if (option.equals("Show History")) {
			Intent intent4 = new Intent(SettingActivity.this, ShowHistoryActivity.class);
			startActivity(intent4);
			overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
		} else if (option.equals("Clear Places")){
			// clear places history
	        searchHistoryHandle.open();
				searchHistoryHandle.clearPlaceHistory();
			searchHistoryHandle.close();
			SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
	                MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
			suggestions.clearHistory();
			
			Toast.makeText(SettingActivity.this, "All places cleared!", 
					Toast.LENGTH_SHORT).show();				
		}

		super.onListItemClick(l, v, position, id);
	}

	public boolean isNetworkOnline() {
	    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
	        return true;
	    }
	    return false;
	}
	
	public void onBackPressed(){		
		super.onBackPressed();
		overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
	}
}